package com.briup.jz.service.impl;

import java.util.Date;
import java.util.List;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.briup.jz.service.IAccountCustomerService;
import com.briup.jz.utils.CustomerException;
import com.briup.jz.bean.AccountCustomer;
import com.briup.jz.bean.AccountCustomerExample;
import com.briup.jz.bean.extend.AccountCustomerExtend;
import com.briup.jz.dao.AccountCustomerMapper;
import com.briup.jz.dao.extend.AccountCustomerExtendMapper;

@Service
public class AccountCustomerServiceImpl implements IAccountCustomerService{
    @Autowired
    private AccountCustomerMapper accountCustomerMapper;
    @Autowired
	private AccountCustomerExtendMapper accountCustomerExtendMapper;
    
    @Override
    public List<AccountCustomer> query(Double transfer_money, String type, String status , Long userId) {
    	AccountCustomerExample example = new AccountCustomerExample();
    	AccountCustomerExample.Criteria criteria = example.createCriteria();
    	
    	if(status !=null) {
			criteria.andStatusEqualTo(status);			
		}
		if(type !=null) {
			criteria.andTypeLike("%"+type+"%");
			
		}
		if(userId != null) {
    		criteria.andUserIdEqualTo(userId);
    	}
		if(transfer_money !=null) {
			criteria.andTransferMoneyEqualTo(transfer_money);
		}
    	
    	return accountCustomerMapper.selectByExample(example);
    }
    
    @Override
    public void saveOrupdate(AccountCustomer accountCustomer) throws CustomerException {

 
    	if (accountCustomer.getId() == null) {
    		 // 初始化属性
        	accountCustomer.setTransferTime(new Date().getTime());
        	accountCustomer.setStatus("未审核");
            // 插入到数据库
            accountCustomerMapper.insert(accountCustomer);
        } else {
            accountCustomerMapper.updateByPrimaryKey(accountCustomer);
        }
    }

    @Override
    public void deleteById(long id) throws CustomerException {
        AccountCustomer accountCustomer = accountCustomerMapper.selectByPrimaryKey(id);
        if (accountCustomer == null) {
            throw new CustomerException("要删除的顾客账单不存在");
        } else {
            accountCustomerMapper.deleteByPrimaryKey(id);
        }
}

	@Override
	public List<AccountCustomerExtend> queryCascade(Double transfer_money, String type, String status, Long userId) {
		// TODO Auto-generated method stub
		return accountCustomerExtendMapper.select(transfer_money, type, status, userId);
	}
    }
